<?php
namespace sm_shop\api\member;
use sm_shop\controller;
//use sm_shop\lib\wx_aes\WXBizDataCrypt;
use sm_shop\model\customerModel;
use sm_shop\model\settingModel;

/**
 * app应用登录
 */
class app extends controller{

    /**
     * APP注册
     */
    public function register(){

        global $_W,$_GPC;
        $res = [
            'status'=>0
        ];

        $app_setting = settingModel::get_app();
        if( !empty( $app_setting['app_id'] ) ){
            $appId = $app_setting['app_id'];
        }else{
            $appId = $_W['account']['key'];
        }

        if( $_GPC['__input'] ){
            $post = $_GPC['__input'];
        }else{
            $post = $_GPC;
        }

//        $post = !empty($post)? json_decode( $post, true) :[];

        $referee = !empty( $post['referee'] ) ? intval($post['referee']) : 0;
//        $userInfo = !empty($post['userInfo'])? json_decode( $post['userInfo'], true) :[];

        if( empty($post['openid']) ){
            $res['status'] = 1;
            $res['description'] = '用户信息获取不到，注册失败';
            $res['params'] = $post;
            $res['openid'] = $post['openid'];
            $res['params_type'] = gettype($post);
            echo json_encode( $res );
            die();
        }

        $data = [
            "openid"    => $post['openid'],
            "nickName"  => $post['nickName'],
            "gender"    => $post['gender'],
            "city"      => $post['city'],
            "province"  => $post['province'],
            "country"   => $post['country'],
            "avatarUrl" => $post['avatarUrl'],
            "headUrl"   => $post['avatarUrl'],
            "unionId"   => $post['unionId'],
            "user_from" => 2, // 用户来自app。 0：公众号，1:小程序，2：app
        ];
        $res['customer_id'] = customerModel::registerByApp( $data, $referee );
        if( empty( $res['customer_id'] ) ){
            $res['status'] =  1;
            $res['description'] = '注册失败';
        }


        echo json_encode( $res );


    }

}